home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / TSFAQP15.ZIP / FAQPAS2.TXT / text0000.txt < prev   
Encoding:
Text File  |  1993-08-18  |  35.5 KB  |  895 lines

  1. FAQPAS2.TXT More frequently (and not so frequently) asked Turbo
  2. Pascal questions with Timo's answers.
  3.  
  4. ..................................................................
  5. Prof. Timo Salmi      Co-moderator of comp.archives.msdos.announce
  6. Moderating  at  garbo.uwasa.fi anonymous FTP archives 128.214.87.1
  7. Faculty of Accounting & Industrial Management; University of Vaasa
  8. Internet:  ts@uwasa.fi  Bitnet:  salmi@finfun; FI-65101,   Finland
  9.  
  10. -------------------------------------------------------------------
  11. 31) How does one store, and then restore the original screen?
  12. 32) How can I convert a TPU unit of one TP version to another?
  13. 33) Which error is e.g. Runtime error 205, etc
  14. 34) Why can't I open read-only files? I get "File access denied".
  15. 35) How do I obtain high and low parts of a byte variable?
  16. 36) How can I set a hi-intensity color background in the text mode?
  17. 37) Where can I find a program to convert (Turbo) Pascal to C?
  18. 38) How can I read input without echoing to the screen?
  19. 39) How can I edit the readln input stream?
  20. 40) How can I write (brand) something into my executables?
  21. 41) What is wrong with my program? It hangs without a clear pattern?
  22. 42) How do I convert a decimal word into a hexadecimal string, etc?
  23. 43) How to determine the last drive?
  24. 44) How can I put a running clock into my Turbo Pascal program?
  25. 45) How to establish if a name refers to a directory or not?
  26. 46) How does one disable alt-ctrl-del?
  27. 47) How can I test whether a file exists?
  28. 48) What is the name of the current Turbo Pascal program?
  29. 49) How is the code for rebooting the PC written in Turbo Pascal?
  30. 50) How can I write inline code?
  31. 51) I am running out of memory when compiling my large program.
  32. 52) How do I avoid scrolling in the last column of the last row?
  33. -------------------------------------------------------------------
  34.  
  35. From ts@uwasa.fi Wed Aug 18 00:00:31 1993
  36. Subject: Saving the screen
  37.  
  38. 31. *****
  39.  Q: How does one store, and then restore the original screen?
  40.  
  41.  A: Here is a simple outline for storing and restoring a text mode
  42. screen. Note that the code below is incomplete in a sense that it
  43. works for a color monitor only, because the monochrome screen
  44. address is $B000:$0000.
  45.    For storing and restoring the graphics screen see Ohlsen & Stoker
  46. (1989), Turbo Pascal Advanced Techniques, Que, pp 333-337.
  47.   uses Crt;
  48.   type ScreenType = array [1..4000] of byte;        (* 2 x 80 x 25 *)
  49.   var ColorScreen : ScreenType Absolute $B800:$0000;
  50.       SavedScreen : ScreenType;
  51.       posx, posy : byte;
  52.   begin
  53.     SavedScreen := ColorScreen;      (* Save the screen *)
  54.     posx := WhereX; posy := WhereY;  (* Save the cursor position *)
  55.     writeln ('A simple demo storing and restoring the color text screen');
  56.     writeln ('By Prof. Timo Salmi, ts@uwasa.fi');
  57.     writeln; write ('Press <-'''); readln;
  58.     ColorScreen := SavedScreen;   (* Restore the screen *)
  59.     GotoXY(posx,posy);            (* Go to the stored cursor position *)
  60.   end.
  61. If you would prefer not using the Crt unit, you can apply WHEREXFN,
  62. WHEREYFN, and GOATXY from TSUNTG.TPU from /pc/ts/tspa33*.zip.
  63. Likewise, if you wish to test for the monitor type, that is choose
  64. between $B800:$0000 and $B000:$0000 bases, you can use
  65.  MONOFN "Is it a monochrome video adapter"
  66. in the said units collection.
  67. --------------------------------------------------------------------
  68.  
  69. From ts@uwasa.fi Wed Aug 18 00:00:32 1993
  70. Subject: Converting TPUs
  71.  
  72. 32. *****
  73.  Q: How can I convert a TPU unit of one TP version to another?
  74.  
  75.  A: Forget it. In practical terms such a conversion is not on. The
  76. Turbo Pascal TPU units are strictly version dependent. If there were
  77. a working solution I assume we would have heard of it long since.
  78. The hacks that have been tried won't solve this dilemma. For all
  79. practical purposes you need the source code and the relevant
  80. compiler version.
  81.    You may nevertheless wish to ascertain for which version a TPU
  82. unit has been compiled. This is very simple. Just look at the first
  83. four character of a TPU file. The codes are
  84.  TPU0  for 4.0
  85.  TPU5  for 5.0
  86.  TPU6  for 5.5
  87.  TPU9  for 6.0
  88.  TPUQ  for 7.0 real mode
  89. But don't go editing these. It will not get you anywhere.
  90. --------------------------------------------------------------------
  91.  
  92. From ts@uwasa.fi Wed Aug 18 00:00:33 1993
  93. Subject: Finding about runtime errors
  94.  
  95. 33. *****
  96.  Q: Which error is e.g. Runtime error 205
  97.  
  98.  A: Basically this is a case of RTFM (read the f*ing manual). But it
  99. is very easy to find out even without resorting to the manual. Put
  100. temporarily the statement RunError (205); as the first statement of
  101. your program. Then run your program from the Turbo Pascal IDE, that
  102. is from within the TP editor. The description of the error will
  103. appear.
  104.    If you run a program from within a Turbo Pascal IDE, it is
  105. advisable to turn on the debug options on. You'll get both the error
  106. number and the description. Furthermore by pressing F1 after the
  107. error you get its description in a more verbal format.
  108.    One further trick is to put "uses TSERR"; (Include verbal
  109. run-time error messages) into your program. If you do that, the
  110. run-time errors will be given with a verbal description not just as
  111. a number. TSERR.TPU is part of my TPU collection /pc/ts/tspa*.zip.
  112. --------------------------------------------------------------------
  113.  
  114. From ts@uwasa.fi Wed Aug 18 00:00:34 1993
  115. Subject: Opening read-only files
  116.  
  117. 34. *****
  118.  Q: Why can't I open read-only files? I get "File access denied".
  119.  
  120.  A: The answer is rather simple, but it is not well displayed in the
  121. manuals. In order to read a read-only file you have to set the
  122. FileMode as 0 like below. Else you'll get runtime error 005 "File
  123. access denied".
  124.   var f      : text;          (* Can be any file type *)
  125.       savefm : byte;
  126.   begin
  127.     savefm := FileMode;       (* Save the current FileMode status *)
  128.     FileMode := 0;            (* The default is 2 *)
  129.     assign (f, 'readonly.txt');
  130.     reset (f);
  131.     { have your wicked ways }
  132.     close (f);
  133.     FileMode := savefm;       (* Restore the original FileMode *)
  134.   end.
  135. --------------------------------------------------------------------
  136.  
  137. From ts@uwasa.fi Wed Aug 18 00:00:35 1993
  138. Subject: Getting a nybble from a byte
  139.  
  140. 35. *****
  141.  Q: I have a variable of type BYTE and would like to extract two
  142. numbers from it. (The first 4 bits making up number A, the second 4
  143. bits making up number B).  How can I extract these two numbers?
  144.  
  145.  A: Ah, this questions bring back the good bad old days of the
  146. Commodore C64 programming when bit operations were rather a rule
  147. than a exception. Here is the solution.
  148.   function HIBYTEFN (x : byte) : byte;
  149.   begin
  150.     hibytefn := x Shr 4;           (* Shift right by four bits *)
  151.   end;
  152.   {}
  153.   function LOBYTEFN (x : byte) : byte;
  154.   begin
  155.     lobytefn := x and 15;          (* x and 00001111 *)
  156.   end;
  157. From Patrick Taylor (exuptr@exu.ericsson.se): Ah, leave it to Timo
  158. to come up with a different way! An other is (n div 16)
  159. (n mod 16).
  160.    Patrick is right.  But unless the compiler is optimized, the
  161. former produces more efficient code. Not that it really makes any
  162. practical difference whatsoever.
  163.    Of course the fastest code is produced using assembler as pointed
  164. out by Maarten Pennings (maarten@cs.ruu.nl) who provided the
  165. following inline example:
  166.   function high(b:byte):byte;
  167.     inline($58         { POP AX      | AH=?, AL=b       }
  168.           /$30/$e4     { XOR AH,AH   | AH=0, AL=b       }
  169.           /$b9/$04/$00 { MOV CX,0004 | AH=0, AL=b, CL=4 }
  170.           /$d3/$e8     { SHR AX,CL   | AX=b shr 4       }
  171.           );
  172.  
  173.  A2: Getting a word from a longint can alternatively be achieved
  174. without any calculations by using a kind of typecasting. Below is
  175. the code I have utilized in garbo.uwasa.fi:/pc/tspa*.zip.
  176.   (* Get the high-order word of the longint argument *)
  177.   function HIWORDFN (x : longint) : word;
  178.   type type1 = record
  179.                  low  : word;
  180.                  high : word;
  181.                end;
  182.   var m1 : type1 absolute x;
  183.   begin
  184.     hiwordfn := m1.high;
  185.   end;  (* hiwordfn *)
  186. --------------------------------------------------------------------
  187.  
  188. From ts@uwasa.fi Wed Aug 18 00:00:36 1993
  189. Subject: Setting hi-intensity background
  190.  
  191. 36. *****
  192.  Q: How can I set a hi-intensity color background in the text mode?
  193.  
  194.  A: As you should know, the you can test for a blinking text for
  195. example as follows.
  196.   uses Crt;
  197.   begin
  198.     TextColor (11 + 128);  (* or LightCyan + Blink *)
  199.     TextBackground (Blue);
  200.     writeln ('What''s the catch?');  (* An aside, note the '' pair *)
  201.   end.
  202. In the above, bit 7 (the 128) controls the blinking. If you have at
  203. least an EGA, you can alter the interpretation of the highest text
  204. color bit to denote a hi-intensity background, but then you lose the
  205. the blinking. The following piece of code disables blinking,
  206. enabling a hi-intensity background.
  207.   uses Dos;
  208.   var regs : registers;
  209.   begin
  210.     FillChar (regs, SizeOf(regs), 0); (* An initialization precaution *)
  211.     regs.ah := $10;                   (* Function $10 *)
  212.     regs.al := $03;                   (* Subfunction $03 *)
  213.     regs.bl := $00;
  214.     Intr ($10, regs);      (* ROM BIOS video driver interrupt *)
  215.   end.
  216. To enable blinking again, set regs.bl := $01; Any high-intensity
  217. background you may have currently on the screen, will instantly
  218. change into a blinking text a a low-intensity background.
  219.  
  220.  A2: The previous answer assumes at least an EGA. Otherwise ports
  221. must be accessed. This is both advanced and dangerous programming,
  222. because errors in handling posts can do real harm. Besides it is
  223. fair to require at least an EGA in writing modern programs, at least
  224. for non-laptops, and on the latter the colors don't really matter
  225. for CGA and below. Let's take a look, nevertheless, how this is done
  226. for a CGA. Note that this won't work an an EGA and beyond, not at
  227. least in my tests. For detecting the video adapter you have, see the
  228. DetectGraph procedure in you Turbo Pascal manual.
  229.    First we need some basics from MEMORY.LST in Ralf Brown's
  230. garbo.uwasa.fi:/pc/programming/inter35b.zip (or whatever version is
  231. current):
  232.  Format of BIOS Data Segment at segment 40h:
  233.   63h WORD Video CRT controller base address: color=03D4h, mono=03B4h
  234.   65h BYTE Video current setting of mode select register 03D8h/03B8h
  235. From David Jurgens's /pc/programming/helppc21.zip we see
  236.   3D0-3DF Color Graphics Monitor Adapter (ports 3D0-3DB are
  237.           write only, see 6845)
  238.   3D8 6845 Mode control register (CGA, EGA, VGA, except PCjr)
  239. From Darryl Friesen's (friesend@jester.usask.ca) in comp.lang.pascal
  240. we have, the following procedure, with my own added comments (* *).
  241.   procedure SetBlinkState (state : boolean);
  242.   var ModeRegPort : word;
  243.       ModeReg     : byte;
  244.   begin
  245.     Inline($FA); { CLI }           (* Interrupts off *)
  246.     ModeRegPort := MemW[$0040:$0063]+4;  (* Typically $03D4+4 = $03D8 *)
  247.     ModeReg := Mem[$0040:$0065];   (* Typically 1001 *)
  248.     if state then                  (* Bit 5 controls blink enable *)
  249.       ModeReg := ModeReg or $20    (* $20 = 00100000 (base2) *)
  250.     else
  251.       ModeReg := ModeReg and $DF;  (* $DF = 11011111 disable *)
  252.     Port[ModeRegPort] := ModeReg;  (* Typically $9 = 00001001 *)
  253.     Mem[$0040:$0065] := ModeReg;   (*       or $29 = 00101001 *)
  254.     Inline($FB) { STI }            (* Interrupts on *)
  255.   end;
  256. --------------------------------------------------------------------
  257.  
  258. From ts@uwasa.fi Wed Aug 18 00:00:37 1993
  259. Subject: Pascal to C
  260.  
  261. 37. *****
  262.  Q: Where can I find a program to convert (Turbo) Pascal to C?
  263.  
  264.  A: This is a relevant question, but I have placed elsewhere the
  265. tips on the "looking for a program" questions. Here are the
  266. pointers to further pointers :-). (The FAQ versions might have been
  267. updated since I wrote this.)
  268.  garbo.uwasa.fi:/pc/pd2/camfaq.zip
  269.  camfaq.zip comp.archives.msdos.(d/announce) FAQ (general finding)
  270.  :
  271.  garbo.uwasa.fi:/pc/pd2/tsfaqn37.zip
  272.  tsfaqn37.zip Questions from UseNet and Timo's answers
  273.  :
  274.  garbo.uwasa.fi:/pc/pd2/faquote.zip
  275.  faquote.zip Old information from tsfaq Frequently Asked Questions
  276. --------------------------------------------------------------------
  277.  
  278. From ts@uwasa.fi Wed Aug 18 00:00:38 1993
  279. Subject: Turning off the input echo
  280.  
  281. 38. *****
  282.  Q: How can I read input without echoing to the screen?
  283.  
  284.  A: It is fairly simple. Study this example source code, with the
  285. manual, if need be.
  286.   uses Crt;
  287.   var password : string;
  288.   {}
  289.   (* Read without echoing *)
  290.   procedure GETPASS (var s : string);
  291.   var key : integer;
  292.       ch : char;
  293.   begin
  294.     s := '';
  295.     repeat
  296.       ch := ReadKey; key := ord (ch);
  297.       case key of
  298.          0 : ch := ReadKey;  (* Discard two-character keys, like F1 *)
  299.         13 : exit;           (* Enter has been pressed *)
  300.         1..12,13..31,255 :;  (* Discard the special characters *)
  301.         else s := s + ch;
  302.       end;
  303.    until false;
  304.   end;  (* getpass *)
  305.   {}
  306.   (* The main program *)
  307.   begin
  308.     write ('Password: ');
  309.     GETPASS (password);
  310.     writeln;
  311.     writeln (password);
  312.   end.
  313.   {}
  314. If you wish to be able to edit the input stream, like having the
  315. BackSpace functional, that is more complicated, and is left as an
  316. exercise after these basics. A hint: 8 : Delete (s, Length(s), 1);
  317. --------------------------------------------------------------------
  318.  
  319. From ts@uwasa.fi Wed Aug 18 00:00:39 1993
  320. Subject: Input line-editing
  321.  
  322. 39. *****
  323.  Q: How can I edit the readln input stream?
  324.  
  325.  A: In practice, if you wish to use anything beyond simple the
  326. BackSpace deleting, you'll have to build your own line editing
  327. routines expanding on the code in the previous item. It is quite a
  328. task, and you can alternatively find the preprogrammed routines in
  329. my Turbo Pascal units tspa33*.zip (or whatever version number is
  330. current).
  331.  EDRDEBLN Editable Readln with ctrl-c, break trapping, pre-fill etc
  332.  EDRDEFLN Editable Readln with recall, pre-fill, and insert toggle
  333.  EDRDLN   Readln with line-editing potential (the simplest)
  334.  EDREABLN Edreadln with ctrl-c and break trapping
  335.  EDREADLN Editable Readln with recall, and insert toggle
  336. --------------------------------------------------------------------
  337.  
  338. From ts@uwasa.fi Wed Aug 18 00:00:40 1993
  339. Subject: Executable branding
  340.  
  341. 40. *****
  342.  Q: How can I write (brand) something into my executables?
  343.     Here is the actual question that led me to writing this item: 'I
  344.     am very interested in the .EXE "branding" techniques you use in
  345.     your TSUNTI unit. Would it be possible to get hold of the source
  346.     code for that unit, as it would save me from having to re-invent
  347.     the wheel?'
  348.  
  349.  A: What you are referring to is
  350.  BRANDEXE Store information within your program's .exe file (MsDos 3.0+)
  351.  CHKSUMFN Checksum selftest to detect any tampering (MsDos 3.0+)
  352.  USECOUNT Get the number of times the program has been used
  353. Sorry no, I don't want to distribute my /pc/turbopas/tspa33*.zip
  354. source codes.  Besides they would be less useful to you than you may
  355. think because internally my programs are in Finnish, comments,
  356. variable and procedure names, and all. But I can hopefully help you
  357. by giving a reference to a similar code.  Please see Ohlsen &
  358. Stoker, Turbo Pascal Advanced Techniques, Que, 1989, p. 420.
  359. --------------------------------------------------------------------
  360.  
  361. From ts@uwasa.fi Wed Aug 18 00:00:41 1993
  362. Subject: Elusive, inconsistent errors
  363.  
  364. 41. *****
  365.  Q: What is wrong with my program? It hangs without a clear pattern?
  366.  
  367.  A: With experience one learns that some programming errors are very
  368. elusive. I have many times seen users declaring that they have found
  369. a bug in Turbo Pascal, but in the overwhelming majority of cases it
  370. still is just a programming error, which just is more difficult to
  371. find than the more clear-cut cases. When you have symptoms like your
  372. program crashing from within the IDE, but working seemingly all
  373. right when called as stand-alone, or something equally strange, you
  374. might have one of the following problems.
  375. - A variable or some variables in your code are uninitialized thus
  376.   getting random values, which differ depending on your environment.
  377. - Your indexes are overflowing. Set on the range check {$R+}
  378.   directive for testing.
  379. - An error in the pointer logic.
  380. Normal debugging does not necessarily help in locating these errors
  381. because one is easily led to debugging the wrong parts of one's
  382. program. Especially the latter two reasons can cause errors which
  383. seemingly have nothing to do with the actual cause. This results
  384. from the fact that indexing and pointer errors can overwrite parts
  385. of memory causing strange quirks in your program. If you have used
  386. indexing with {$R-} or if you use pointer operations, sooner or
  387. later you are bound to have these problems in developing your
  388. applications.
  389. --------------------------------------------------------------------
  390.  
  391. From ts@uwasa.fi Wed Aug 18 00:00:42 1993
  392. Subject: Converting the number base
  393.  
  394. 42. *****
  395.  Q: How do I convert a decimal word into a hexadecimal string, etc?
  396.  
  397.  A: Here is one possibility
  398.   function HEXFN (decimal : word) : string;
  399.   const hexDigit : array [0..15] of char = '0123456789ABCDEF';
  400.   begin
  401.     hexfn := hexDigit[(decimal shr 12)]
  402.           + hexDigit[(decimal shr 8) and $0F]
  403.           + hexDigit[(decimal shr 4) and $0F]
  404.           + hexDigit[(decimal and $0F)];
  405.   end;  (* hexfn *)
  406. Here is another conversion example (from longint to binary string)
  407.   function LBINFN (decimal : longint) : string;
  408.   const BinDigit : array [0..1] of char = '01';
  409.   var i     : byte;
  410.       binar : string;
  411.   begin
  412.     FillChar (binar, SizeOf(binar), ' ');
  413.     binar[0] := chr(32);
  414.     for i := 0 to 31 do
  415.       binar[32-i] := BinDigit[(decimal shr i) and 1];
  416.     lbinfn := binar;
  417.   end;  (* lbinfn *)
  418. For a full set of conversions, both from and to decimal, apply
  419. TSUTNTB.TPU from garbo.uwasa.fi:/pc/ts/tspa*.zip.
  420. --------------------------------------------------------------------
  421.  
  422. From ts@uwasa.fi Wed Aug 18 00:00:43 1993
  423. Subject: Identifying the last drive
  424.  
  425. 43. *****
  426.  Q: How to determine the last drive?
  427.  
  428.  A: One way of doing that is utilizing the information in DPB, that
  429. is the Drive Parameter Block, but that is rather complicated, so you
  430. can find that without source code in garbo.uwasa.fi:/pc/ts/tspa*.zip
  431. in the TSUNTH unit.
  432.  Another way is using interrrupt 21H, function 36H to detect if a
  433. drive exists starting from the first drive letter. The code is given
  434. below. The disadvantage of this method is that it does not
  435. distinguish between real and substituted drives.
  436.   uses Dos;
  437.   function LASTDFN : char;  (* Detect last harddisk letter *)
  438.   var regs : registers;
  439.       i    : byte;
  440.   begin
  441.     i := 2;
  442.     repeat
  443.       Inc(i);
  444.       FillChar (regs, SizeOf(regs), 0);
  445.       regs.ah := $36;
  446.       regs.dl := i;
  447.       MsDos(regs);
  448.     until (regs.ax = $FFFF);
  449.     lastdfn := chr(i+63);
  450.   end;  (* lastdfn *)
  451. --------------------------------------------------------------------
  452.  
  453. From ts@uwasa.fi Wed Aug 18 00:00:44 1993
  454. Subject: Clock display in a TP program
  455.  
  456. 44. *****
  457.  Q: How can I put a running clock into my Turbo Pascal program?
  458.  
  459.  A: We are not speaking of a stand-alone TSR-clock (which is a
  460. different task), but considering a clock that continuously displays
  461. the time in some part of the output screen of your Turbo Pascal
  462. program.
  463.     You might first want to read the earlier items about ReadKey
  464. usages if you are not familiar with it (you probably are, because
  465. you would not pose this advanced question if you were a novice). The
  466. items are the unlikely "How do I disable or capture the break key in
  467. Turbo Pascal?" and "How can I read input without echoing to the
  468. screen?"
  469.    The general idea is to make the body of the program a repeat
  470. until loop using ReadKey for input and updating the clock display
  471. at suitable junctions within the loop. The scheme is thus something
  472. like the following.
  473.   procedure showtime;
  474.     begin
  475.       { if the second has changed, write the time }
  476.     end;
  477.   :
  478.   repeat
  479.     { do whatever }
  480.     showtime;
  481.     if KeyPressed then
  482.       case ReadKey of
  483.         { whatever }
  484.         { exit rules }
  485.       end;
  486.     showtime;
  487.     :
  488.     showtime;
  489.   until false;
  490.    One trick of the trade is that you must not update your clock
  491. each time the clock routine is encountered. You should test if the
  492. second has changed, and update only then. Else you are liable to get
  493. an annoying flicker in your clock.
  494. --------------------------------------------------------------------
  495.  
  496. From ts@uwasa.fi Wed Aug 18 00:00:45 1993
  497. Subject: Is a name a directory
  498.  
  499. 45. *****
  500.  Q: How to establish if a name refers to a directory or not?
  501.  
  502.  A: This question has turned out a bit more complicated than I first
  503. thought. There are several methods, each with some catch. The first
  504. is trying to open the name as a file and observing the IOResult. The
  505. ISDIRFN function in garbo.uwasa.fi:/pc/ts/tspa*.zip TPU unit
  506. TSUNTJ.TPU is based on this method. Unfortunately it is not always
  507. stable. I have been reported problems in connection with DRDOS by
  508. Richard Breuer (ricki@pool.informatik.rwth-aachen.de) who has
  509. tested these routines.
  510.   The second method (ISDIR2FN) is based on the fact that the file
  511. NUL exists in a directory if the directory exists.
  512.   The thrid method (ISDIR3FN) is a brute force method. It is given
  513. below, since it is quite an instructive little exercise of Turbo
  514. Pascal programming.
  515.   (* Search recursively through a drive's directories.
  516.      Auxiliary, recursive procedure for ISDIR3FN *)
  517.   procedure SEARCHDR (Path, FileSpec : string;
  518.                       name           : string;
  519.                       var found      : boolean);
  520.   var FileInfo : SearchRec;
  521.   begin
  522.     FindFirst (Path + '*.*', Directory, FileInfo);
  523.     while DosError = 0 do
  524.       begin
  525.         if ((FileInfo.Attr and Directory) > 0) and
  526.             (FileInfo.Name <> '.') and
  527.             (FileInfo.Name <> '..') then
  528.               begin
  529.                 SEARCHDR (Path + FileInfo.Name + '\',
  530.                           FileSpec,
  531.                           name,
  532.                           found);
  533.                 if Path + FileInfo.Name + '\' = name then
  534.                   found := true;
  535.               end;
  536.         FindNext (FileInfo);
  537.       end; {while}
  538.   end;  (* searchdr *)
  539.  
  540.   (* Does a name refer to a directory *)
  541.   function ISDIR3FN (name : string) : boolean;
  542.   var drive : char;
  543.       found : boolean;
  544.   begin
  545.     {... Default value ...}
  546.     isdir3fn := false;
  547.     {... Discard empty names ...}
  548.     if name = '' then exit;
  549.     {... Expand into a fully qualified name, makes it uppercase ...}
  550.     name := FExpand (name);
  551.     if name[Length(name)] <> '\' then name := name + '\';
  552.     {... Extract the drive letter from the name ...}
  553.     drive := UpCase (name[1]);
  554.     {... Check first for the root ...}
  555.     if drive + ':\' = name then
  556.       begin isdir3fn := true; exit; end;
  557.     {... Check the rest of the directories recursively ...}
  558.     found := false;
  559.     SEARCHDR (drive + ':\', '*.*', name, found);
  560.     isdir3fn := found;
  561.   end;  (* isdir3fn *)
  562. --------------------------------------------------------------------
  563.  
  564. From ts@uwasa.fi Wed Aug 18 00:00:46 1993
  565. Subject: Disabling alt-ctrl-del
  566.  
  567. 46. *****
  568.  Q: How does one disable alt-ctrl-del?
  569.  
  570.  A: I can only give a pointer to source code. Take a look at the
  571. code by Mikko Hanninen in garbo.uwasa.fi:/pc/turbopas/cadthf10.zip.
  572.    I have utilized alt-ctrl-del disabling at least in one of my own
  573. programs (PESTIKID.EXE). The code is not available, but the general
  574. idea is replacing the old keyboard interrupt ($09) with a handler of
  575. one's own. If the handler detects alt-ctrl-del, the keyboard is
  576. reset, else the handler is chained back to the original interrupt.
  577. The chaining requires a rather complicated inline procedure provided
  578. in TurboPower Software's kit. An additional complication is that the
  579. del keypress must be intercepted already at the relevant port $60,
  580. and the alt and ctrl status must be tested, so that the rebooting
  581. will not be invoked. Resetting the keyboard requires accessing the
  582. $20 and $61 ports.
  583. --------------------------------------------------------------------
  584.  
  585. From ts@uwasa.fi Wed Aug 18 00:00:47 1993
  586. Subject: Does a file exist
  587.  
  588. 47. *****
  589.  Q: How can I test whether a file exists?
  590.  
  591.  A: There are several alternatives. Here is the most common with
  592. example code. It recognizes also read-only, hidden and system files.
  593.   function FILEXIST (name : string) : boolean;
  594.   var fm : byte;
  595.       f  : file;
  596.       b  : boolean;
  597.   begin
  598.     fm := FileMode;
  599.     FileMode := 0;
  600.     assign (f, name);
  601.     {$I-} reset(f); {$I+}
  602.     b := IOResult = 0;
  603.     if b then close(f);
  604.     filexist := b;
  605.     FileMode := fm;
  606.   end;
  607.  
  608. A second alternative is
  609.   Uses Dos;
  610.   function FILEXIST (name : string) : boolean;
  611.   var f  : file;
  612.       a  : word;
  613.   begin
  614.     assign (f, name);
  615.     GetFAttr (f, a);
  616.     filexist := false;
  617.     if DosError = 0 then
  618.       if ((a and Directory) = 0) and ((a and VolumeId) = 0) then
  619.         filexist := true;
  620.   end;
  621.  
  622. A third alternative is
  623.   Uses Dos;
  624.   function FILEXIST (name : PathStr) : boolean;
  625.   begin
  626.     filexist := FSearch (name, '') <> '';
  627.   end;
  628.  
  629. A fourth alternative is the following. Be careful with this option,
  630. since it works a bit differently from the others. It accepts wild
  631. cards. Thus, for example FILEXIST('c:\autoexec.*') would be TRUE in
  632. this method, while FALSE in all the above.
  633.   Uses Dos;
  634.   function FILEXIST (name : string) : boolean;
  635.   var f : SearchRec;
  636.   begin
  637.     filexist := false;
  638.     FindFirst (name, AnyFile, f);
  639.     if DosError = 0 then
  640.       if (f.attr <> Directory) and (f.attr <> VolumeId) then
  641.         filexist := true;
  642.   end;
  643. A good variation from KDT@newton.national-physical-lab.co.uk of this
  644. theme, disallowing wildcards:
  645.   function file_exists (fname :string) :boolean;
  646.   var f :searchrec;
  647.   begin
  648.     findfirst (fname, anyfile - directory - volumeid, f);
  649.     file_exists := (doserror + pos('*',fname) + pos('?',fname) = 0);
  650.   end;
  651.  
  652. --------------------------------------------------------------------
  653.  
  654. From ts@uwasa.fi Wed Aug 18 00:00:48 1993
  655. Subject: The current program name
  656.  
  657. 48. *****
  658.  Q: What is the name of the current Turbo Pascal program?
  659.  
  660.  A: The name of the currently executing Turbo Pascal program is in
  661. ParamStr(0).
  662.    This was introduced in TP version 5.0, and as far as I recall at
  663. least MsDos version 3.0 is required. For TP 4.0 you can use
  664. "ParamStr0 The name of the program" from TSUNT45 in garbo.uwasa.fi:
  665. /pc/ts/tspa3340.zip (or whatever the version number is the latest).
  666.    It is advisable to put the value into a string variable at be
  667. beginning of the program before eny I/O takes place. Thus you might
  668. wish to use:
  669.   var progname : string;
  670.   begin  { the main program }
  671.     progname := ParamStr(0);
  672.     :
  673. A bonus of this method is that you can access the individual
  674. characters of progname (e.g. progname[1] for the drive) while that
  675. is not possible to do for the ParamStr keyword.
  676. --------------------------------------------------------------------
  677.  
  678. From ts@uwasa.fi Wed Aug 18 00:00:49 1993
  679. Subject: How can a program reboot my PC?
  680.  
  681. 49. *****
  682.  Q: How is the code for rebooting the PC written in Turbo Pascal?
  683.  
  684.  A: This item draws from the information and the C-code example in
  685. Stan Brown's comp.os.msdos.programmer FAQ, garbo.uwasa.fi:
  686. /pc/doc-net/faqp9312.zip (at the time of writing this), from
  687. memory.lst and interrup.b in /pc/programming/inter35b.zip, and from
  688. /pc/programming/helppc21.zip. The Turbo Pascal code is my adaptation
  689. of the C-code. It is not a one-to-one replica.
  690.    The usually advocated warm-boot method is storing $1234 in the
  691. word at $0040:$0072 and jumping to address $FFFF:$0000. The problem
  692. with this approach is that files must first be closed, potential
  693. caches flushed. This is how to do this
  694.   procedure REBOOT;
  695.   label next;
  696.   var regs  : registers;
  697.       i     : byte;
  698.       ticks : longint;
  699.   begin
  700.     {... "press" alt-ctrl ...}
  701.     mem[$0040:$0017] := mem[$0040:$0017] or $0C;  { 00001100 }
  702.     {... "press" del, try a few times ...}
  703.     for i := 1 to 10 do
  704.       begin
  705.         FillChar (regs, sizeOf(regs), 0);  { initialize }
  706.         regs.ah := $4F;  { service number }
  707.         regs.al := $53;  { del key's scan code }
  708.         regs.flags := FCarry;  { "sentinel for ignoring key" }
  709.         Intr ($15, regs);
  710.         {... check if the del key registered, if not retry ...}
  711.         if regs.flags and Fcarry > 0 then goto next;
  712.         {... waste some time, watch out for midnight ...}
  713.         ticks := MemL [$0040:$006C];
  714.         repeat until (MemL[$0040:$006C] - ticks > 3) or
  715.                      (MemL[$0040:$006C] - ticks < 0)
  716.     end; {for}
  717.     exit;
  718.   next:
  719.     {... disk reset: writes all modified disk buffers to disk ...}
  720.     FillChar (regs, sizeOf(regs), 0);
  721.     regs.ah := $0D;
  722.     MsDos (regs);
  723.     {... set post-reset flag, use $0000 instead of $1234 for coldboot ...}
  724.     memW[$0040:$0072] := $1234;
  725.     {... jump to $FFFF:0000 BIOS reset ...}
  726.     Inline($EA/$00/$00/$FF/$FF);
  727.   end;  (* reboot *)
  728. One slight problem with this approach is that the keyboard intercept
  729. interrupt $15 service $4F requires at least an AT according to
  730. inter35b.zip. A simple test based on "FFFF:E byte ROM machine id"
  731. (the previous definition is from helppc21.zip) is:
  732.   function ISATFN : boolean;
  733.   begin
  734.      case Mem[$F000:$FFFE] of
  735.        $FC, $FA, $F8 : isatfn := true;
  736.        else isatfn := false;
  737.      end; {case}
  738.   end;  (* isatfn *)
  739. For a more comprehensive test use CPUFN "Get the type of the
  740. processor chip" from TSUNTH in garbo.uwasa.fi:/pc/ts/tspa*.zip.
  741. --------------------------------------------------------------------
  742.  
  743. From ts@uwasa.fi Wed Aug 18 00:00:50 1993
  744. Subject: Writing inline code
  745.  
  746. 50. *****
  747.  Q: How can I write inline code?
  748.  
  749.  A: In Turbo Pascal versions prior 6.0 assembler code could not be
  750. directly included in the code. Instead one had to assemble the code
  751. into inline statements. Consider the task of rebooting the PC
  752. (without disk closing and cache flushing).  The assemble code for
  753. this is
  754.   mov ax,40
  755.   mov ds,ax
  756.   mov wo [72],1234
  757.   jmp FFFF:0000
  758. To assemble this code into an inline statement write the following
  759. file calling it e.g. debug.in.  The empty line is important.
  760.   .... begin debug.in, cut here ....
  761.   a 100
  762.   mov ax,40
  763.   mov ds,ax
  764.   mov wo [72],1234
  765.   jmp FFFF:0000
  766.  
  767.   u 100
  768.   q
  769.   .... end debug.in, cut here ....
  770. Give the following command
  771.   debug < debug.in
  772. You'll get
  773.   0E9E:0100 B84000        MOV     AX,0040
  774.   0E9E:0103 8ED8          MOV     DS,AX
  775.   0E9E:0105 C70672003412  MOV     WORD PTR [0072],1234
  776.   0E9E:010B EA0000FFFF    JMP     FFFF:0000
  777. This translates into
  778.     Inline ($B8/$40/$00/
  779.             $8E/$D8/
  780.             $C7/$06/$72/$00/$34/$12/
  781.             $EA/$00/$00/$FF/$FF);
  782. --------------------------------------------------------------------
  783.  
  784. From ts@uwasa.fi Wed Aug 18 00:00:51 1993
  785. Subject: Out of memory in compiling
  786.  
  787. 51. *****
  788.  Q: I am running out of memory when compiling my large program. What
  789.     can I do?
  790.  
  791.  A: If you are compiling your program from within the IDE (the
  792. Integrated Development Environment) then select invoke the Option
  793. from the main menu, choose the Compiler item and set the Link buffer
  794. to Memory. (Also make the Compile option Destination to be Disk).
  795.    If this is not sufficient, next resort to using the TPC command
  796. line version of the Turbo Pascal compiler instead of the IDE.  Use
  797. the /L option ("Link buffer on disk").  Other users have also
  798. pointed to using the protected mode version, TPCX, if you have it.
  799.    Divide your program into units. It is advisable anyway for
  800. modularity when your program size grows.
  801.    I have no experience with this alternative, but other users have
  802. pointed that "If you have some extended memory, switch to BP", that
  803. is Borland Pascal 7.0.
  804.  
  805.  A2: If you would prefer compiling your program from within the IDE
  806. but cannot do it for the above reason (or if you would prefer to
  807. compile your program from within your favorite editor instead of the
  808. TP IDE) you can use the following trick. If your editor has a macro
  809. language like most good editors do, the assign a hotkey macro that
  810. compiles the current file with the TPC. If you are using SemWare's
  811. QEdit editor you'll find such a macro in garbo.uwasa.fi:/pc/ts/
  812. tsqed17.zip ("Macros and configurations for QEdit text-editor").
  813.    Also your editor must be swapped to disk during the compilation
  814. if memory is critical. There is a very good program for doing that:
  815. /pc/sysutil/shroom2d.zip ("Shell Room, Swap to disk when shelling to
  816. application"). For example I invoke the QEdit editor with using the
  817. following batch:
  818.  c:\tools\shroom -s r:\cmand -z 1024 c:\qedit\q %1 %2 %3 %4 %5 %6 %7
  819. You'll find more about the switches in the Shell Room documentation.
  820. The -s switch designates the swap destination (my r:\cmand directory
  821. is on my ramdisk). The -z switch sets the shell environment size.
  822.   An unfortunate part is that the Turbo Pascal IDE is about the only
  823. program I know that is not amenable the to Shell Room utility, so
  824. you cannot utilize Shell Room to swap the TP IDE to disk.
  825. --------------------------------------------------------------------
  826.  
  827. From ts@uwasa.fi Wed Aug 18 00:00:52 1993
  828. Subject: Last position write woes
  829.  
  830. 52. *****
  831.  Q: How do I avoid scrolling in the last column of the last row?
  832.  
  833.  A: If you use write or writeln at the last column of the last row
  834. (usually 80,25) the screen will scroll. If you wish to avoid the
  835. scrolling you'll have to use an alternative write that does not move
  836. the cursor. Here is a procedure to write without moving the cursor
  837.   uses Dos;
  838.   procedure WriteChar (Character : char; fgColor, bgColor : byte);
  839.   var r : registers;
  840.   begin
  841.     FillChar (r, SizeOf(r), 0);
  842.     r.ah := $09;
  843.     r.al := ord(Character);
  844.     r.bl := (bgColor shl 4) or fgColor;
  845.     r.cx := 1;    { Number of repeats }
  846.     Intr ($10, r);
  847.   end;  (* writechar *)
  848. Thus, if you wish to write to the last column of the last row, you
  849. must first move the cursor to that position. That can be done in
  850. alternative ways. One might get there by having written previously
  851. on the screen (with writeln and write routines) until one is in that
  852. position. Another alternative is using GoToXY(80,20), but then you
  853. have to use the Crt unit. If you don't want to use it, then you can
  854. move the cursor by employing "GOATXY As the ordinary GoToXY but no
  855. Crt unit required" from garbo.uwasa.fi:/pc/ts/ tspa*.zip.
  856.    There is an alternative interrupt service ($0A) which does the
  857. same as service $09, but uses the default colors instead. Just
  858. substitute $0A for $09, and leave the r.bl assignment out of the
  859. WriteChar routine.
  860.    Another option for writing anyhere on the screen without
  861. affecting the cursor is using direct screen writes:
  862.   uses Dos;
  863.   procedure WriteChar (c : char; x, y : byte; fg, bg : byte);
  864.   var vidstart : word;
  865.       regs     : registers;
  866.   begin
  867.     FillChar (regs, SizeOf(regs), 0);
  868.     regs.ah := $0F;
  869.     Intr ($10, regs);  { Color or MonoChrome video adapter }
  870.     if regs.al = 7 then vidstart := $B000 else vidstart := $B800;
  871.     mem[vidstart:((y-1)*80+x-1)*2] := ord(c);
  872.     mem[vidstart:((y-1)*80+x-1)*2+1] := (bg shl 4) or fg;
  873.   end;
  874. To write to the last postion simply apply e.g.
  875.   WriteChar ('X', 80, 25, 14, 0);  { Yellow on black }
  876. The foreground (fg) and the background (bg) color codes are
  877.   Black        =   0
  878.   Blue         =   1
  879.   Green        =   2
  880.   Cyan         =   3
  881.   Red          =   4
  882.   Magenta      =   5
  883.   Brown        =   6
  884.   LightGray    =   7
  885.   DarkGray     =   8
  886.   LightBlue    =   9
  887.   LightGreen   =  10
  888.   LightCyan    =  11
  889.   LightRed     =  12
  890.   LightMagenta =  13
  891.   Yellow       =  14
  892.   White        =  15
  893.   Blink        = 128
  894. --------------------------------------------------------------------
  895.